package com.zhou.qianguo.main

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.google.android.material.tabs.TabLayout
import com.xuexiang.xpage.annotation.Page
import com.zhou.qianguo.BuildConfig
import com.zhou.qianguo.R
import com.zhou.qianguo.databinding.ActivityMainBinding
import com.zhou.qianguo.hall.TaskHallFragment
import com.zj.easy.ui.base.BaseActivity
import com.zj.splash.SplashActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


@Page(name = "首页")
class MainActivity : BaseActivity<ActivityMainBinding>() {
    val TAB_TASK_HALL = 0
    val TAB_MINE_TASK = 1
    val TAB_MSG = 2
    val TAB_MINE = 3

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //开启splash界面
        if (!BuildConfig.DEBUG) {
            startActivity(Intent(this, SplashActivity::class.java))
        }
        GlobalScope.launch(Dispatchers.Main) {
            delay(1000L)
            //清除背景,防止过度绘制
            window.decorView.setBackgroundColor(Color.TRANSPARENT)
        }

        //状态栏字体深色
        window.decorView.systemUiVisibility =
            window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

//        ImmersionBar.with(this)
//            .transparentStatusBar()           //透明状态栏和导航栏，不写默认状态栏为透明色，导航栏为黑色（设置此方法，fullScreen()方法自动为true）
//            .navigationBarColor(R.color.transparent) //导航栏颜色，不写默认黑色
//            .navigationBarAlpha(0.0f)  //导航栏透明度，不写默认0.0F
//            .init()

        initTab()
    }

    /**
     * 初始化Tab
     */
    private fun initTab() {
        getTabView("任务大厅", R.drawable.icon_main_tab_hall)
        getTabView("我的任务", R.drawable.icon_main_tab_task)
        getTabView("消息", R.drawable.icon_main_tab_msg)
        getTabView("我的", R.drawable.icon_main_tab_mine)
        switchPage(TaskHallFragment::class.java)
        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                when (tab.position) {
                    TAB_TASK_HALL -> switchPage(TaskHallFragment::class.java)
                    TAB_MINE_TASK -> switchPage(TaskHallFragment::class.java)
                    TAB_MSG -> switchPage(TaskHallFragment::class.java)
                    TAB_MINE -> switchPage(TaskHallFragment::class.java)
                    else -> {
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
    }

    // Tab自定义view
    fun getTabView(title: String, image_src: Int): View? {
        val tab=binding.tabs.newTab()
        tab.setText(title)
        tab.setIcon(image_src)
        val v = LayoutInflater.from(applicationContext).inflate(R.layout.view_main_tab_item, null)
        tab.setCustomView(v)
        binding.tabs.addTab(tab)
        return v
    }

    override fun getBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

}